//
//  ZDBActivityViewController.m
//  ZDB
//
//  Created by Kk on 2020/3/14.
//  Copyright © 2020 ZDB. All rights reserved.
//

#import "ZDBActivityViewController.h"
#import "Activity.h"
#import "ZDBActivityTableViewCell.h"
#import "ZDBActivityHeadView.h"
#import "ZDBVideoPlayerViewController.h"

@interface ZDBActivityViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)NSMutableArray *dataSource;
@property (nonatomic, strong) UITableView * tableView;
@property (nonatomic,strong)ZDBActivityHeadView * tableviewHeadView;
@property (nonatomic,strong)UIView * footView;
@end

@implementation ZDBActivityViewController
- (void)viewWillAppear:(BOOL)animated
{
     [self statusBarStyleIsDark:NO];
    [self.navigationController setNavigationBarHidden:YES animated:NO]; //设置隐藏
    
}
- (void)viewWillDisappear:(BOOL)animated
{
     [self statusBarStyleIsDark:YES];
    [self.navigationController setNavigationBarHidden:NO animated:NO]; //设置隐藏
    [super viewWillDisappear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
     [self signInfoRequestData];
}
-(void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    self.tableviewHeadView.frame = CGRectMake(0, 0, UIScreenWidth, 210+IPhoneTop);
 
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.tableView];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(LogOutSusess) name:ExitLogonNotificationse object:nil];
    _tableviewHeadView = [[NSBundle mainBundle] loadNibNamed:@"ZDBActivityHeadView" owner:self options:nil].lastObject;
    _tableviewHeadView.frame = CGRectMake(0, 0, UIScreenWidth, 210+IPhoneTop);
    WeakSelf
    _tableviewHeadView.signInblock = ^(UIButton * _Nonnull sender) {
        [weakSelf signInButtonClick:sender];
    };
    
    _tableView.tableHeaderView =_tableviewHeadView;
    [self showLoadingAnimation];
}
- (void)LogOutSusess
{
    [self.tableviewHeadView.totalLab countFrom:0 to:0.0 withDuration:1];
    [self.tableviewHeadView LogOutSusess];
}
#pragma 用户签到
- (void)signInButtonClick:(UIButton *)sender
{
    WeakSelf
     [self showLoadingAnimation];
    NSString  * userSign = [[YINetWorkAPIGenerate sharedInstance] APINomark:@"userSign"];
    [[SmileHttpTool sharedInstance] POST:userSign parameters:@{} origin:NO success:^(NSInteger statusCode, NSString *message, id responseObject) {
         [self stopLoadingAnimation];
        if (statusCode ==0) {
            [weakSelf signInfoRequestData];
        }else
        {
            [ZBDUnit showMessage:message];
        }
       
    } failure:^(id responseObject, NSError *error) {
        [self stopLoadingAnimation];
    }];
}
-(void)usergetUserSeeAdvertisement{
    
    WEAKSELF
    if (![DJLoginHelper sharedInstance].is_Login) {
        [[DJLoginHelper sharedInstance] dj_showLoginVC:self];
        return;
    }
    NSString  * signInfo = [[YINetWorkAPIGenerate sharedInstance] APINomark:@"usergetUserSeeAdvertisement"];
    [[SmileHttpTool sharedInstance] POST:signInfo parameters:@{} origin:NO success:^(NSInteger statusCode, NSString *message, id responseObject) {
         [self stopLoadingAnimation];
        STRONGSELFFor(weakSelf);
        if (statusCode==0) {
            NSDictionary * dic = [NSDictionary dictionaryWithDictionary:responseObject];
            NSLog(@"usergetUserSeeAdvertisement----%@",dic);
            ZDBVideoPlayerViewController * player = [ZDBVideoPlayerViewController new];
            player.modalPresentationStyle = 0;
            player.infoDicc = dic;
            [strongSelf presentViewController:player animated:YES completion:nil];
        }else{
           
            [JMNotifyView showNotify:message isSuccessful:NO];
            
        }
       
    } failure:^(id responseObject, NSError *error) {
        [self stopLoadingAnimation];
    }];
    
    
}

//获取数据
- (void)signInfoRequestData
{
    
    WeakSelf
    if (![DJLoginHelper sharedInstance].is_Login) {
         [self stopLoadingAnimation];
        return;
    }
    NSString  * signInfo = [[YINetWorkAPIGenerate sharedInstance] APINomark:@"signInfo"];
    [[SmileHttpTool sharedInstance] POST:signInfo parameters:@{} origin:NO success:^(NSInteger statusCode, NSString *message, id responseObject) {
         [self stopLoadingAnimation];
        NSDictionary * dic = responseObject;
        if (!kDictIsEmpty(dic)) {
            
            NSString * integral = dic[@"integral"];//签到余额
            NSString * isTodaySign = dic[@"isTodaySign"];//今日是否签到 1未签到 2已签到
            NSString * signCont = dic[@"signCont"];//签到天数
            NSArray * signInfoList = dic[@"signInfoList"];
             [weakSelf.tableviewHeadView.totalLab countFrom:0 to:integral.floatValue withDuration:1];
            [weakSelf.tableviewHeadView getSignCont:signCont.integerValue wihtIsTodaySign:isTodaySign.integerValue signInfoList:signInfoList];
        }else
        {
             [ZBDUnit showMessage:message];
        }
       
    } failure:^(id responseObject, NSError *error) {
        [self stopLoadingAnimation];
    }];
}
- (NSMutableArray *)dataSource
{
    if (!_dataSource) {
        _dataSource = [NSMutableArray new];
    }
    return _dataSource;
}
#pragma  mark -----懒加载
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, UIScreenWidth, UIScreenHeight  - TABBAR_HEIGHT)style:UITableViewStyleGrouped];
        _tableView.tableFooterView = [UIView new];
        _tableView.separatorColor = CELLCOLOR;
        _tableView.delegate = self;
        _tableView.dataSource=self;
        _tableView.backgroundColor =  VIEWBGCOLOR;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.alwaysBounceVertical = NO;
        _tableView.bounces = NO;
        [_tableView registerNib:[UINib nibWithNibName:@"ZDBActivityTableViewCell" bundle:nil]  forCellReuseIdentifier:@"ZDBActivityTableViewCell"];
        _tableView.tableFooterView = self.footView;
    }
    return _tableView;
}
- (UIView *)footView
{
    if (!_footView) {
        _footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 160)];
        _footView.backgroundColor = VIEWBGCOLOR;
        UIView * view = [[UIView alloc] initWithFrame:CGRectMake(15, 0, UIScreenWidth-30, 160)];
        view.backgroundColor = WHITECOLOR;
        KViewRadius(view, 4);
        UILabel * titleLab = [[UILabel alloc] initWithFrame:CGRectMake(12, 12, 200, 26)];
        titleLab.text = @"积分说明";
        titleLab.textColor = MAINTITLECOLOR;
        titleLab.font = AutoFont(15);
        [view addSubview:titleLab];
        UILabel * content = [[UILabel alloc] initWithFrame:CGRectMake(12, titleLab.bottom,  view.width-24, 0)];
        
        NSString * str  = @"一、签到说明\n   1.每日用户可参与一次签到，获得当日签到积分奖励，多次参与不获得奖励；\n   2.用户累计签到七日可获得额外奖励,额外积分奖励额度可在用户中心，积分明细内查看；\n   3.用户签到以及累计签到积分奖励实时发放；\n   4.禁止所有使用非法工具进行签到，获取奖励，一经发现将永久封号；\n本活动解释权在法律范围内归本公司所有。\n\n二、观看广告说明\n   1.每日用户可以再活动中心参与观看广告；\n   2.进入观看广告界面之后倒计时未到0秒则退出该页面则无法获得积分奖励；\n   3.观看广告积分奖励实时发放，每次观看可获得20-88积分奖励；\n   4.观看次数已超过系统设置每日观看次数，继续观看广告不会继续获得积分奖励；\n   5.禁止所有使用非法工具进行广告观看，获取奖励，一经发现将永久封号；\n本活动解释权在法律范围内归本公司所有。";
        content.attributedText = [ZBDUnit finderattributedLinesString:str attributedString:str color:MAINTITLECOLOR1 font:AutoFont(11)];
        content.textColor = MAINTITLECOLOR1;
        content.font = AutoFont(11);
        content.numberOfLines = 0;
        [content sizeToFit];
        [view addSubview:content];
        content.width = view.width-24;
        content.height = content.height;
        view.height =54+ content.height;
        _footView.height =view.height;
        
        [_footView addSubview:view];
    }
    return _footView;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 90;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZDBActivityTableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:@"ZDBActivityTableViewCell" forIndexPath:indexPath];
    [cell getDataWithcellForRowAtIndexPath:indexPath];
    if (indexPath.row==1) {
        
        WEAKSELF
             [cell.lookTaskBtn add_BtnClickHandler:^(NSInteger tag) {
                 STRONGSELFFor(weakSelf);
                 [strongSelf usergetUserSeeAdvertisement];
             }];
        
    }
    return cell;
}
@end
